package com.acme.bankapp.service.command;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.exception.command.CommandException;
import com.acme.bankapp.exception.bank.ClientExistsException;
import com.acme.bankapp.exception.bank.FeedFormatException;
import com.acme.bankapp.service.bank.BankFeeder;

/**
 * @author Gregory Sobko
 */
public class AddClientCommand implements Command {

    private Bank bank;

    public AddClientCommand(Bank bank) {
        this.bank = bank;
    }

    @Override
    public String getCommandPrefix() {
        return "add";
    }

    @Override
    public String executeCommand(String feedString) throws CommandException {
        try {
            Client client = BankFeeder.parseClientFromLine(feedString);
            bank.addClient(client);
            return "Client " + client.getName() + " was added!";
        } catch (ClientExistsException | FeedFormatException e) {
            throw new CommandException("Exception occurred on command executing: " + e.getMessage(), e);
        }
    }

}
